package com.cai.servlet.user;

import com.cai.dao.UserDao;
import com.cai.dao.impl.UserDaoImpl;
import com.cai.pojo.UserLogin;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Objects;

/**
 * @author RuoLi
 * 普通用户登录请求
 * 验证转发
 */
@WebServlet(name = "ServletUserLogin", value = "/User/UserLogin")
public class ServletUserLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String userAccount = request.getParameter("userAccount");
        String password = request.getParameter("userPassword");
        UserDao userDao = new UserDaoImpl();
        HttpSession session = request.getSession();
        int count = userDao.userLogin(userAccount, password);
        if (count != -1) {
            //身份
            String identity = null;
            //登录成功，开始验证身份
            for (UserLogin userLogin : userDao.verifyIdentity(userAccount, password)
            ) {
                identity=userLogin.getLogin_identity();
            }
            session.setAttribute("userAccount",userAccount);
            session.setAttribute("PersonType",identity);
            switch(Objects.requireNonNull(identity)) {
                case "PersonType2":
                    //转发到出题者
                    //登录主页
                    response.getWriter().println(2);
                    break;
                case "PersonType3":
                    //转发到管理员
                    //转发给管理员 登录
                    response.getWriter().println(3);
                    break;
                default:
                    //转发到做题者
                    //登录主页
                    response.getWriter().println(1);
                    break;
            }
        }else {
            response.getWriter().println(0);
        }
    }
}
